package com.yiren.dbaa.module.practice_set.map.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.currency.utils.GpsUtil;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.basestation.bean.BaseStationBean;
import com.yiren.dbaa.module.practice_set.map.contract.AddMapContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/28 18:52
 * @description: 新增地图
 */
public class AddMapModel implements AddMapContract.Model {
    private Activity activity;
    private Gson gson;

    public AddMapModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void getShareMap(String city, String user, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadMapByPid("?pid=" + city + "&user=" + user), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<MapEntity>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<MapEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    if (testBaseBean.getData() != null) {
                        List<MapEntity> list = new ArrayList<>();
                        if (StringUtils.isNotEmpty(user)) {
                            for (int i = 0; i < testBaseBean.getData().size(); i++) {
                                if (String.valueOf(testBaseBean.getData().get(i).getUserId()).equals(user)) {
                                    list.add(testBaseBean.getData().get(i));
                                }
                            }
                        } else {
                            list.addAll(testBaseBean.getData());
                        }
                        callBack.success(list);
                    } else {
                        callBack.fail("当前城市查询结果为空!");
                    }
                } else {
                    callBack.fail("查询失败!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });
    }

    @Override
    public void getBaseStationList(CommonCallBack callBack) {
        int distance = 10000;   //加载附近10公里范围内的基站
        double curLongitude = GpsInfoBean.getInstance().getLongitude();
        double curLatitude = GpsInfoBean.getInstance().getLatitude();
        double secondLon = GpsUtil.getDistanceLon(distance, curLatitude);
        double secondLat = GpsUtil.getDistanceLat(distance);
        double minLon = Double.parseDouble(String.format("%.8f", curLongitude - secondLon));
        double maxLon = Double.parseDouble(String.format("%.8f", curLongitude + secondLon));
        double minLat = Double.parseDouble(String.format("%.8f", curLatitude - secondLat));
        double maxLat = Double.parseDouble(String.format("%.8f", curLatitude + secondLat));
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.getBaseStationList("?minLon=" + minLon + "&maxLon=" + maxLon + "&minLat=" + minLat + "&maxLat=" + maxLat), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<BaseStationBean>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<BaseStationBean>>>() {
                }.getType());
                callBack.success(testBaseBean.getData());
            }
        });
    }

    @Override
    public boolean isHaveData(MapEntity map, List<MapEntity> mapListSer) {
        boolean isHaveData = false;
        //通过名字判断服务器是否存在
        if (mapListSer != null) {
            for (MapEntity mapEntity : mapListSer) {
                if (map.getName().equals(mapEntity.getName())) {
                    isHaveData = true;
                    break;
                }
            }
        }
        //通过名字判断本地是否存在
        List<MapEntity> mapList = UserDbController.getInstance(false).queryAll(MapEntity.class);
        for (MapEntity mapEntity : mapList) {
            if (map.getName().equals(mapEntity.getName())) {
                isHaveData = true;
                break;
            }
        }
        return isHaveData;
    }
}
